package utils;

import controller.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class InsertUser extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String str = "select u_id from user_info where u_phone="+phone;
        try {
            Connection connection = DBUtils.Connection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(str);
            if (!resultSet.next()){
                String strr = "insert into user_info(u_name,u_phone,u_password) values(?,?,?)";
                PreparedStatement preparedStatement = connection.prepareStatement(strr);
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,phone);
                preparedStatement.setString(3,password);
                preparedStatement.executeUpdate();
                //System.out.println("aaaaaaa");
                req.getRequestDispatcher("/chenggong").forward(req,resp);
            }else{
                req.getRequestDispatcher("/shibai").forward(req,resp);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
